/**
 * 
 */
package optimization.naturalOptimization.population;

import java.util.ArrayList;

import optimization.naturalOptimization.NaturalOptimization;
import optimization.naturalOptimization.population.individual.Individual;

/**
 * This class provides the administration of individuals for the optimization
 * algorithms.
 * 
 * @author Kevin Wagner
 * @version 1.0
 */
public class Population {

	@SuppressWarnings("rawtypes")
	private ArrayList<Individual> individuals = new ArrayList<Individual>();
	private NaturalOptimization parentOptimization;

	/**
	 * Creates a new Population with a given parent optimization algorithm.
	 * 
	 * @param no
	 *            {@link NaturalOptimization}
	 */
	public Population(NaturalOptimization no) {
		this.parentOptimization = no;
	}

	/**
	 * Returns a list with all individuals of the population.
	 * 
	 * @return list of individuals
	 */
	@SuppressWarnings("rawtypes")
	public ArrayList<Individual> getAllIndividuals() {
		return individuals;
	}

	/**
	 * Returns the number of individuals in the population.
	 * 
	 * @return number of individuals
	 */
	public int getNumberOfIndividuals() {
		return individuals.size();
	}

	/**
	 * Adds the given individual to the population.
	 * 
	 * @param individual
	 *            new {@link Individual}
	 */
	@SuppressWarnings("rawtypes")
	public void addIndividual(Individual individual) {
		individuals.add(individual);
	}

	/**
	 * Removes all individuals.
	 */
	@SuppressWarnings("rawtypes")
	public void removeAllIndividuals() {
		this.individuals = new ArrayList<Individual>();
	}

	/**
	 * Removes the individual with the given index from the population.
	 * 
	 * @param index
	 */
	public void removeIndividual(int index) {
		individuals.remove(index);
	}

	/**
	 * Returns the Optimization that uses the population.
	 * 
	 * @return {@link NaturalOptimization}
	 */
	public NaturalOptimization getParentOptimization() {
		return parentOptimization;
	}

	/**
	 * Returns the individual with the given index. If the index is to high or
	 * to low, an exception will be thrown.
	 * 
	 * @param index
	 *            number in list
	 * @return the individual with given index
	 */
	@SuppressWarnings("rawtypes")
	public Individual getIndividual(int index) {
		if (index < 0 || index >= getNumberOfIndividuals()) {
			throw new ArrayIndexOutOfBoundsException();
		}
		return individuals.get(index);
	}
	
}
